package com.ruoyi.system.interAttr.attr;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class THWAttr implements AdapterAttr {


    public static void main(String[] args) {
        SalePartner salePartner = new SalePartner();
        salePartner.setSaleKey("e573ec6ce2d14f6fb791ab7ff42e571d");
        salePartner.setOther("iWNo8M1ZC8xD89ryUmtIm3WELQhK6sdgH+ZhxZIXWpI=");
        salePartner.setTwoOther("https://www.taohaowan.com/prod-api/");
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        // 创建一个Map，用于存储需要排序并加密的数据
        Map<String, Object> dataMap = new TreeMap<>();
        dataMap.put("gameId", "c5541e352778443a954b181d5bf0f728");//查询游戏ID
        dataMap.put("appKey", salePartner.getOther());
        dataMap.put("timestamp", time);
        // 遍历排序后的Map，将value值拼接
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
            stringBuilder.append(entry.getValue());
        }
        // 执行SHA-256加密
        String encryptedData = DigestUtil.sha256Hex(stringBuilder.toString());
        dataMap.remove("timestamp");
        dataMap.remove("appKey");

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appId", salePartner.getSaleKey());
        paramMap.put("timestamp", time);
        paramMap.put("sign", encryptedData);
        paramMap.put("data", dataMap);
        Map<String, Object> header = new HashMap<>();
        header.put("Content-Type", "application/json; charset=UTF-8");
        String response = HttpUtils.sendPost("https://www.taohaowan.com/prod-api/system-api/gamesInfo/getGameAttr", JSONObject.toJSONString(paramMap), "application/json");
        System.out.println(response);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String url = "https://www.taohaowan.com/prod-api/system-api/gamesInfo/gameSearch";
        String appId = salePartner.getSaleKey();
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        Map<String, Object> dataMap = new TreeMap<>();
        dataMap.put("appKey", salePartner.getOther());
        dataMap.put("timestamp", time);
        // 遍历排序后的Map，将value值拼接
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
            stringBuilder.append(entry.getValue());
        }
        // 执行SHA-256加密
        String encryptedData = DigestUtil.sha256Hex(stringBuilder.toString());
        dataMap.remove("timestamp");
        dataMap.remove("appKey");

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appId", appId);
        paramMap.put("timestamp", time);
        paramMap.put("sign", encryptedData);
        paramMap.put("data", dataMap);
        Map<String, Object> header = new HashMap<>();
        header.put("Content-Type", "application/json; charset=UTF-8");
        String result = HttpUtils.sendPost(url, JSONObject.toJSONString(paramMap), "application/json");
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("gameName");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        // 创建一个Map，用于存储需要排序并加密的数据
        Map<String, Object> dataMap = new TreeMap<>();
        dataMap.put("gameId", gameId);//查询游戏ID
        dataMap.put("appKey", salePartner.getOther());
        dataMap.put("timestamp", time);
        // 遍历排序后的Map，将value值拼接
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
            stringBuilder.append(entry.getValue());
        }
        // 执行SHA-256加密
        String encryptedData = DigestUtil.sha256Hex(stringBuilder.toString());
        dataMap.remove("timestamp");
        dataMap.remove("appKey");

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appId", salePartner.getSaleKey());
        paramMap.put("timestamp", time);
        paramMap.put("sign", encryptedData);
        paramMap.put("data", dataMap);
        Map<String, Object> header = new HashMap<>();
        header.put("Content-Type", "application/json; charset=UTF-8");
        String response = HttpUtils.sendPost("https://www.taohaowan.com/prod-api/system-api/gamesInfo/gameAreaSearch", JSONObject.toJSONString(paramMap), "application/json");
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        String uuid = "";
        Map<String,Object> uuidMap = new HashMap<>();
        for (JSONObject datum : data) {
            uuid = getUUid(uuidMap);
            InterServerRes interServer = new InterServerRes();
            interServer.setUuid(uuid);
            interServer.setId(datum.getString("id"));
            interServer.setName(datum.getString("areaName"));
            String servers = datum.getString("childrenNode");
            if(StringUtils.isNotEmpty(servers)) {
                List<InterServerRes> interServerRes1 = new ArrayList<>();
                List<JSONObject> jsonObjects = JSONObject.parseArray(servers, JSONObject.class);
                for (JSONObject object : jsonObjects) {
                    uuid = getUUid(uuidMap);
                    InterServerRes interServer1 = new InterServerRes();
                    interServer1.setUuid(uuid);
                    String id1 = object.getString("id");
                    String title1 = object.getString("areaName");
                    interServer1.setId(id1);
                    interServer1.setName(title1);
                    interServerRes1.add(interServer1);
                }
                interServer.setInterServerRes(interServerRes1);
            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        // 创建一个Map，用于存储需要排序并加密的数据
        Map<String, Object> dataMap = new TreeMap<>();
        dataMap.put("gameId", gameId);//查询游戏ID
        dataMap.put("appKey", salePartner.getOther());
        dataMap.put("timestamp", time);
        // 遍历排序后的Map，将value值拼接
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
            stringBuilder.append(entry.getValue());
        }
        // 执行SHA-256加密
        String encryptedData = DigestUtil.sha256Hex(stringBuilder.toString());
        dataMap.remove("timestamp");
        dataMap.remove("appKey");

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appId", salePartner.getSaleKey());
        paramMap.put("timestamp", time);
        paramMap.put("sign", encryptedData);
        paramMap.put("data", dataMap);
        Map<String, Object> header = new HashMap<>();
        header.put("Content-Type", "application/json; charset=UTF-8");
        String response = HttpUtils.sendPost("https://www.taohaowan.com/prod-api/system-api/gamesInfo/getGameAttr", JSONObject.toJSONString(paramMap), "application/json");
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        String data2 = JSONObject.parseObject(response).getString("data");
        Object o1 = JSONArray.parseArray(data2).get(0);
        String children = JSONObject.parseObject(o1.toString()).getString("children");
        List<JSONObject> data = JSONObject.parseArray(children, JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("2")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("0")||d.getString("type").equals("1")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("3")).collect(Collectors.toList());
        }
        Map<String,Object> uuidMap = new HashMap<>();
        String uuid = "";
        for (JSONObject object : collect) {
            uuid = getUUid(uuidMap);
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setId(object.getString("id"));
            interAttrRes.setUuid(uuid);
            interAttrRes.setName(object.getString("label"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            String string = object.getString("children");
            if(StringUtils.isNotEmpty(string)) {
                JSONArray jsonArray = JSONArray.parseArray(string);
                for (Object o : jsonArray) {
                    uuid = getUUid(uuidMap);
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setUuid(uuid);
                    interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("id"));
                    interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("label"));
                    String children2 = JSONObject.parseObject(o.toString()).getString("children");
                    if(StringUtils.isNotEmpty(children2)) {
                        JSONArray jsonArray2 = JSONArray.parseArray(children2);
                        List<InterAttrRes> interAttrResList2 = new ArrayList<>();
                        for (Object p : jsonArray2) {
                            uuid = getUUid(uuidMap);
                            InterAttrRes interAttrRes2 = new InterAttrRes();
                            interAttrRes2.setUuid(uuid);
                            interAttrRes2.setId(JSONObject.parseObject(p.toString()).getString("id"));
                            interAttrRes2.setName(JSONObject.parseObject(p.toString()).getString("label"));
                            interAttrResList2.add(interAttrRes2);
                        }
                        interAttrRes1.setInterServerRes(interAttrResList2);
                    }
                    interAttrResList1.add(interAttrRes1);
                    interAttrRes.setInterServerRes(interAttrResList1);
                }
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }

    String getUUid(Map<String,Object> uuidMap){
        String uuid = UUID.randomUUID().toString();
        while(Objects.nonNull(uuidMap.get(uuid))){
            uuid = UUID.randomUUID().toString();
        }
        uuidMap.put(uuid,"1");
        return uuid;
    }
}